package cn.tips.dmp.system.user.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.tips.dmp.common.constant.SystemAssistConstant;
import cn.tips.dmp.common.sms.captcha.service.CaptchaService;
import cn.tips.dmp.supports.result.ApiResultDTO;
import cn.tips.dmp.supports.result.ApiResultGenerator;
import cn.tips.dmp.supports.status.ApiStatusCode;
import cn.tips.dmp.supports.util.AESUtil;
import cn.tips.dmp.system.assist.log.annotation.OperLog;
import cn.tips.dmp.system.user.dto.SystemUserLoginParamsDto;
import cn.tips.dmp.system.user.entity.SystemUserPO;
import cn.tips.dmp.system.user.service.SystemUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author pkb
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/tipsApi/system/user")
public class SystemUserController {

    private final SystemUserService systemUserService;

    private final CaptchaService captchaService;

    private final StringRedisTemplate stringRedisTemplate;

    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.SYSTEM, operClassify = SystemAssistConstant.systemAssistClassify.SECURITY, operModule = SystemAssistConstant.systemAssistModule.LOGIN, operDesc = "", recordRequestParams = false)
    @RequestMapping("/login")
    public ApiResultDTO login(@RequestBody @Valid SystemUserLoginParamsDto dto, HttpServletRequest request) {

        //验证码是否正确
        String key = captchaService.getImgCaptchaKey(request);
        String redisCaptcha = stringRedisTemplate.opsForValue().get(key);

        if (redisCaptcha != null) {
            if (!redisCaptcha.toUpperCase().equals(dto.getCaptcha().toUpperCase())) {
                return ApiResultGenerator.create(ApiStatusCode.CAPTCHA_ERROR.getCode(), ApiStatusCode.CAPTCHA_ERROR.getDescription());
            }
        } else {
            return ApiResultGenerator.create(ApiStatusCode.CAPTCHA_EXPIRED.getCode(), ApiStatusCode.CAPTCHA_EXPIRED.getDescription());
        }

        String phoneNumber = AESUtil.decryptLogin(dto.getPhoneNumber());

        SystemUserPO systemUserPo = systemUserService.readOneByPhoneNumber(phoneNumber);
        if (null == systemUserPo) {
            // 没该用户
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_NOT_EXISTED.getCode(), ApiStatusCode.ACCOUNT_NOT_EXISTED.getDescription());
        }
        //是否被锁定
        if(systemUserPo.getLocked()){
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_LOCKED.getCode(), ApiStatusCode.ACCOUNT_LOCKED.getDescription());
        }
        //比较密码
        if(systemUserPo.getPassword().equals(dto.getPassword())){
            StpUtil.login(systemUserPo.getId());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return ApiResultGenerator.create("0", "登录成功!", tokenInfo.getTokenName()+tokenInfo.getTokenValue());
        }

        return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_INVALID.getCode(), ApiStatusCode.ACCOUNT_INVALID.getDescription());
    }

    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}
